package com.sunlake.spring.main.model.account.mapper;

import com.sunlake.spring.main.model.account.po.RechargeBills;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @author liuhanzhi49
 * @description 针对表【recharge_bills】的数据库操作Mapper
 * @createDate 2025-09-23 15:10:48
 * @Entity com.sunlake.spring.main.model.account.po.RechargeBills
 */
@Mapper
public interface RechargeBillsMapper {

    int deleteByPrimaryKey(Long id);

    int insert(RechargeBills record);

    int insertSelective(RechargeBills record);

    RechargeBills selectByPrimaryKey(Long id);

    int updateByPrimaryKeySelective(RechargeBills record);

    int updateByPrimaryKey(RechargeBills record);

    /**
     * 根据用户ID查询充值记录
     */
    @Select("SELECT * FROM recharge_bills WHERE user_id = #{userId} ORDER BY created_at DESC")
    List<RechargeBills> selectByUserId(@Param("userId") Integer userId);

    /**
     * 根据充值单号查询充值记录
     */
    @Select("SELECT * FROM recharge_bills WHERE recharge_no = #{rechargeNo}")
    RechargeBills selectByRechargeNo(@Param("rechargeNo") String rechargeNo);

    /**
     * 根据用户ID和状态查询充值记录
     */
    @Select("SELECT * FROM recharge_bills WHERE user_id = #{userId} AND status = #{status} ORDER BY created_at DESC")
    List<RechargeBills> selectByUserIdAndStatus(@Param("userId") Integer userId, @Param("status") String status);

    /**
     * 更新充值记录状态
     */
    @Update("UPDATE recharge_bills SET status = #{status}, transaction_no = #{transactionNo}, completed_at = #{completedAt}, updated_at = #{updatedAt} WHERE recharge_no = #{rechargeNo}")
    int updateStatusByRechargeNo(@Param("rechargeNo") String rechargeNo,
                                 @Param("status") String status,
                                 @Param("transactionNo") String transactionNo,
                                 @Param("completedAt") String completedAt,
                                 @Param("updatedAt") String updatedAt);
    /**
     * 查询过期的待支付订单
     */
    @Select("SELECT * FROM recharge_bills WHERE status = 'PENDING' AND created_at < DATE_SUB(NOW(), INTERVAL 120 MINUTE)")
    List<RechargeBills> selectExpiredPendingOrders();
}